// import { createRouter, createWebHistory } from 'vue-router'
// import Login from '@/views/Login/index.vue'
// import Layout from '@/views/Layout/index.vue'
// import Home from '@/views/Home/index.vue'
// // import Category from '@/views/Category/index.vue'
// import SubCategory from '@/views/SubCategory/index.vue'
// import Detail from '@/views/Detail/index.vue'
// import CartList from '@/views/CartList/index.vue'
// import Checkout from '@/views/Checkout/index.vue'
// import Pay from '@/views/Pay/index.vue'
// import PayBack from '@/views/Pay/PayBack.vue'
// import Member from '@/views/Member/index.vue'
// import UserInfo from '@/views/Member/components/UserInfo.vue'
// import UserOrder from '@/views/Member/components/UserOrder.vue'
// import Search from "@/views/Search/index.vue";
// import OrderDetail from "@/views/Order/OrderDetail.vue";
// import UserAddress from "@/views/Member/components/UserAddress.vue";
// import Cookies from 'js-cookie'
// const router = createRouter({
//   history: createWebHistory(import.meta.env.BASE_URL),
//   routes: [
//     {
//
//       path: '/',
//       component: Layout,
//       children: [
//         {
//           path: '',
//           component: Home
//         },
//         // {
//         //   path: 'category/:id',
//         //   component: Category
//         // },
//         {
//           path: 'category/:id',
//           component: () => import('@/views/category/index.vue')
//         },
//         {
//           path: 'category/sub/:id',
//           component: SubCategory
//         },
//         {
//           path: 'detail/:id',
//           component: Detail
//         },
//         {
//           path: '/cartlist',
//           // ✅ 正确写法（使用 @ 别名）
//           component: CartList
//         },
//         {
//           path: 'checkout',
//           component: Checkout
//         },
//         {
//           path: 'pay',
//           component: Pay
//         },
//         {
//           path: 'payBack', // 注意路径，必须是paycallback
//           component: PayBack
//         },
//         {
//           path: 'member',
//           component: Member,
//           children: [
//             {
//               path: '',
//               component: UserInfo
//             },
//             {
//               path: 'order',
//               component: UserOrder
//             },
//             {
//               path: 'address',
//               component: UserAddress
//
//             }
//           ]
//         }, // 新增搜索路由（作为 Layout 的子路由）
//         // {
//         //   path: '/goods-list',
//         //   name: 'GoodsList',
//         //   component: () => import('@/views/GoodsList.vue'),
//         //   meta: { title: '商品列表' }
//         // },
//         {
//           path: '/search',
//           component: Search
//         }
//       ]
//     },
//     {
//       path: '/login',
//       component: Login
//     },
//     // router.js
//     {
//       path: '/order/detail/:orderNo',
//       component: OrderDetail,
//       meta: { requiresAuth: true }
//     }
//   ],
//   // 路由切换时，页面回到顶部
//   scrollBehavior() {
//     return {
//       top: 0
//     }
//   }
// })
// // 添加全局路由守卫
// router.beforeEach((to, from, next) => {
//   // 不需要认证的白名单路径
//   const publicPaths = ['/login', '/', '/category', '/detail']
//
//   // 检查是否需要认证
//   const requiresAuth = to.matched.some(record => record.meta.requiresAuth)
//
//   // 获取认证信息
//   const hasAuth = Cookies.get('memberId') && Cookies.get('username')
//
//   // 认证逻辑处理
//   if (requiresAuth && !hasAuth) {
//     next({
//       path: '/login',
//       query: { redirect: to.fullPath } // 携带跳转来源
//     })
//   } else if (to.path === '/login' && hasAuth) {
//     next('/') // 已登录用户访问登录页时跳转首页
//   } else {
//     next() // 正常放行
//   }
// })
//
// export default router
// router/index.js
import {createRouter, createWebHistory} from 'vue-router'
import Cookies from 'js-cookie'

// 组件导入
import Login from '@/views/Login/index.vue'
import Layout from '@/views/Layout/index.vue'
import Home from '@/views/Home/index.vue'
import SubCategory from '@/views/SubCategory/index.vue'
import Detail from '@/views/Detail/index.vue'
import CartList from '@/views/CartList/index.vue'
import Checkout from '@/views/Checkout/index.vue'
import Pay from '@/views/Pay/index.vue'
import PayBack from '@/views/Pay/PayBack.vue'
import Member from '@/views/Member/index.vue'
import UserInfo from '@/views/Member/components/UserInfo.vue'
import UserOrder from '@/views/Member/components/UserOrder.vue'
import Search from "@/views/Search/index.vue";
import OrderDetail from "@/views/Order/OrderDetail.vue";
import UserAddress from "@/views/Member/components/UserAddress.vue";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            component: Layout,
            children: [
                {
                    path: '',
                    component: Home
                },
                {
                    path: 'category/:id',
                    component: () => import('@/views/category/index.vue')
                },
                {
                    path: 'category/sub/:id',
                    component: SubCategory
                },
                {
                    path: 'detail/:id',
                    component: Detail
                },
                {
                    path: '/cartlist',
                    component: CartList,
                    meta: {requiresAuth: true}
                },
                {
                    path: 'checkout',
                    component: Checkout,
                    meta: {requiresAuth: true}
                },
                {
                    path: 'pay',
                    component: Pay,
                    meta: {requiresAuth: true}
                },
                {
                    path: 'payBack',
                    component: PayBack,
                    meta: {requiresAuth: true}
                },
                {
                    path: 'member',
                    component: Member,
                    meta: {requiresAuth: true},
                    children: [
                        {
                            path: '',
                            component: UserInfo
                        },
                        {
                            path: 'order',
                            component: UserOrder
                        },
                        {
                            path: 'address',
                            component: UserAddress
                        }
                    ]
                },
                {
                    path: '/search',
                    component: Search
                }
            ]
        },
        {
            path: '/login',
            component: Login,
            meta: {guestOnly: true} // 仅允许未登录用户访问
        },
        {
            path: '/order/detail/:orderNo',
            component: OrderDetail,
            meta: {requiresAuth: true}
        }
    ],
    scrollBehavior() {
        return {top: 0}
    }
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
    // 获取认证状态
    const isAuthenticated = Cookies.get('memberId') && Cookies.get('username')
    if (to.path === '/' && !isAuthenticated) {
        return next('/login')
    }
    // 需要认证的路由检查
    if (to.matched.some(record => record.meta.requiresAuth)) {
        if (!isAuthenticated) {
            next({
                path: '/login',
                query: {redirect: to.fullPath}
            })
        } else {
            next()
        }
    }
    // 已登录用户禁止访问登录页
    else if (to.matched.some(record => record.meta.guestOnly)) {
        if (isAuthenticated) {
            next('/')
        } else {
            next()
        }
    }
    // 其他情况正常放行
    else {
        next()
    }
})

export default router